package cn.passlink.bletoothchat.mActivity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import cn.passlink.bletoothchat.BleGATT.ClientUtils;
import cn.passlink.bletoothchat.R;
import cn.passlink.bletoothchat.SqlTables.MyDatabaseHelper;
import cn.passlink.bletoothchat.mFragment.AddFriendsScrollingFragment;
import cn.passlink.bletoothchat.mFragment.AddGroupsScrollingFragment;
import cn.passlink.bletoothchat.mFragment.myViewModel;

public class AddFriendsActivity extends AppCompatActivity {
   private List<BluetoothDevice> myscannedDevices = new ArrayList<>();
   private ClientUtils clientUtils;
    private myViewModel myViewModel;
    private SQLiteDatabase database;
    private MyDatabaseHelper databaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friends);
        ImageView backButton=(ImageView) findViewById(R.id.add_friends_back_button);
        Button friendsButton= (Button) findViewById(R.id.add_friends_button);
        Button groupsButton= (Button) findViewById(R.id.add_groups_button);
        myViewModel = new ViewModelProvider(this).get(myViewModel.class);
        // 初始画面
        AddFriendsScrollingFragment mAddFriendsScrollingFragment = new AddFriendsScrollingFragment();
        AddGroupsScrollingFragment mAddGroupsScrollingFragment = new AddGroupsScrollingFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();

// 设置初始选项卡
        fragmentManager.beginTransaction()
                .replace(R.id.addFragment, mAddFriendsScrollingFragment)
                .commit();

        clientUtils = new ClientUtils(this, this);
        backButton.setOnClickListener(v -> goToTargetActivity());
        friendsButton.setOnClickListener(v -> {
            // 切换到朋友选项卡
            fragmentManager.beginTransaction()
                    .replace(R.id.addFragment, mAddFriendsScrollingFragment)
                    .commit();
            // 刷新设备
                scanDevices();
            myViewModel.setDevices(myscannedDevices);
        });
        groupsButton.setOnClickListener(v -> {
            // 切换到群组选项卡
            fragmentManager.beginTransaction()
                    .replace(R.id.addFragment, mAddGroupsScrollingFragment)
                    .commit();
            // 刷新设备
            try{
                scanDevices();
            }
            catch (Exception e){
                Toast.makeText(this, "扫描失败，请检查权限和蓝牙是否打开！！", Toast.LENGTH_SHORT).show();
            }

            myViewModel.setDevices(myscannedDevices);
        });

//初始化数据库
        databaseHelper = new MyDatabaseHelper(this, 2);
        database = databaseHelper.getWritableDatabase();
    }


    // 在Activity或Fragment的onResume方法中注册广播接收器
    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter("BleStateBroadcast");
        registerReceiver(BleState, filter);

        try{
            scanDevices();
        }
        catch (Exception e){
            Toast.makeText(this, "扫描失败，请检查权限和蓝牙是否打开！！", Toast.LENGTH_SHORT).show();
        }

        myViewModel.setDevices(myscannedDevices);
    }

//用于工具类传递
    public ClientUtils getClientUtils() {
        return this.clientUtils;
    }

    // 请求蓝牙和蓝牙管理权限


    // 创建广播接收器
    private BroadcastReceiver BleState = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 处理接收到的广播消息
            if (intent.getAction().equals("BleStateBroadcast")) {
                String message = intent.getStringExtra("message");
                switch (message){
                    case "100":
                        Toast.makeText(context, "蓝牙连接中。。。" + message, Toast.LENGTH_SHORT).show();break;
                    case "200":
                        Toast.makeText(context, "蓝牙连接SERVER成功可以尝试聊天了！！！" + message, Toast.LENGTH_SHORT).show();break;
                    case "500":
                        Toast.makeText(context, "断开中 " + message, Toast.LENGTH_SHORT).show();break;
                    case "400":
                        Toast.makeText(context, "蓝牙已断开！！！！ " + message, Toast.LENGTH_SHORT).show();break;
                    case "1000":
                        Toast.makeText(context, "蓝牙连接CLIENT成功可以尝试聊天了！！！！ " + message, Toast.LENGTH_SHORT).show();break;

                }


            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销广播接收器
        unregisterReceiver(BleState);
    }
    public void goToTargetActivity() {
        Intent intent = new Intent(this, RelationshipActivity.class);
        startActivity(intent);
    }
    private void scanDevices(){
        clientUtils.startScan();

    }

    public void setMyscannedDevices(List<BluetoothDevice> myscannedDevices) {
        myViewModel.setDevices(myscannedDevices);
    }



}